#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"xh.h"

struct Node {
	ElementType data;
	node Next;
};

struct Linkqueue {
	node front;
	node rear;
};

Queue creatQueue() {
	Queue Q = (Queue)malloc(sizeof(struct Linkqueue));
	if (Q == NULL) {
		perror("malloc");
		return NULL;
	}
	else {
		Q->front = Q->rear = NULL;
	}
	return Q;
}

int IsEmpty(Queue Q) {
	return Q->front == NULL;
}

void Enqueue(Queue Q, ElementType x) {
	node N=(node)malloc(sizeof(node));
	if (N == NULL) {
		perror("malloc");
		return;
	}
	else {
		N->data = x;
		N->Next = NULL;
		if (Q->rear == NULL) {
			Q->front = N;
			Q->rear = N;
		}
		else {
			Q->rear->Next = N;
			Q->rear = N;
		}
	}
}

void Dequeue(Queue Q) {
	if (IsEmpty(Q)) {
		printf("IsEmpety queue");
		return NULL;
	}
	else {
		node tmp = Q->front;
		if (Q->front == Q->rear)
			Q->front = Q->rear = NULL;
		else
			Q->front = Q->front->Next;
		free(tmp);
	}
}

ElementType First(Queue Q) {
	if (!IsEmpty(Q)) {
		return Q->front->data;
	}
	printf("IsEmpety queue");
	return 0;
}

//ElementType FrontAndDequeue(Queue Q) {
//	int data = 0;
//	if (IsEmpty(Q))
//		printf("Empty queue");
//	else {
//		node tmp= Q->front;
//		data = tmp->data;
//		if (Q->front == Q->rear)
//			Q->front = Q->rear = NULL;
//		else
//			Q->front = Q->front->Next;
//		free(tmp);
//	}
//	return data;
//}

void showqueue(Queue Q) {
	node curr = Q->front;
	while (curr->Next != NULL) {
		printf("%d ", curr->data);
		curr = curr->Next;
	}
	printf("%d\n", curr->data);
}

